
/*
import com.alipay.api.domain.BookTime;
import com.gsnd.gsnd22estore.exception.BookStoreException;
import com.gsnd.gsnd22estore.mapper.EsUserMapper;
import com.gsnd.gsnd22estore.utils.JsonWebTokenUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.SpinnerUI;
import javax.xml.ws.handler.Handler;
@Component
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {
    //验证操作
    @Autowired
    EsUserMapper userMapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)
            throws Exception {


        System.out.println("拦截器执行了");
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NOT_LOGIN);
        }
        Claims claims = null;
        try {
            claims = JsonWebTokenUtils.parseJWT(token);
        }
        catch (SignatureException e)
        {
            //捕获令牌失效
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_TOKEN_IS_INVALID);
        }catch (ExpiredJwtException e)
        {
            //捕获令牌过期
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_LOGIN_TIMEOUT);
        }
        String userId = claims.getId();
        String username = claims.getSubject();

        log.info("{id}-{}登陆成功,");
        return true;
    }

}

import com.gsnd.gsnd22estore.exception.BookStoreException;
import com.gsnd.gsnd22estore.utils.JsonWebTokenUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
@Slf4j
public class UserLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 日志输出
        log.info("后台管理系统登陆校验拦截器拦截到请求,请求路径为 : {}",request.getRequestURL());
        // 检查是否是预请求，如果是直接放行
        String method = request.getMethod();
        if(method.equals("OPTIONS")){
            return true;
        }
        // 登录校验需要什么逻辑呢？
        // 1、检查请求头中有没有token（令牌）
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            // 抛出异常，说token不存在 约等于 用户未登录
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NOT_LOGIN);
        }

        // 2、检查token是否合法
        Claims claims = JsonWebTokenUtils.parseJWT(token);
        String userId = claims.getSubject();

        // 3、如果token合法，解析出其中的用户id，保存在request中
        request.setAttribute("userId",userId);

        // 返回的结果代表是否放行
        return true;
    }
}*/
package com.gsnd.gsnd22estore.web.controller.interceptor;
import com.gsnd.gsnd22estore.exception.BookStoreException;
import com.gsnd.gsnd22estore.mapper.EsUserMapper;
import com.gsnd.gsnd22estore.utils.JsonWebTokenUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
@Slf4j
public class UserLoginInterceptor implements
        HandlerInterceptor {
    @Autowired
    EsUserMapper userMapper;
    @Override
    public boolean
    preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        System.out.println("拦截器执⾏了-------------------");
        // 检查是否是预请求，如果是直接放⾏
        String method = request.getMethod();
        if(method.equals("OPTIONS")){
            return true;
        }
        // 在请求到达资源前进⾏拦截
// 拦截之后检查请求头⾥有没有令牌
        String token = request.getHeader("token");
        if(StringUtils.isEmpty(token)){
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_IS_NOT_LOGIN);
        }
        // 令牌是否合法，解析，这⼀步jwt⼯具本身会
        Claims claims = null;
        try {
            claims = JsonWebTokenUtils.parseJWT(token);
        }catch (SignatureException e){
            // 捕获令牌失效的异常
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_TOKEN_IS_INVALID);
        }catch (ExpiredJwtException e){
            // 捕获令牌过期的异常
            throw new BookStoreException(BookStoreException.CodeMsgEnum.USER_LOGIN_TIMEOUT);
        }
        // 提取出令牌的信息
        String userId = claims.getId();
        String username = claims.getSubject();

        request.setAttribute("userId",userId);
        // 验证操作可加可不加，加了之后可以加分
// 课堂上加个⽇志输出（内置）

        log.info("{}-{}登陆成功！",userId,username);
                // true代表放⾏，false就代表拦截
        return true;
    }
}

